#!/bin/bash

filename=$1
batch_size=$2
let batch_size_flag=batch_size-1

if [ ! $1 ];then
    echo "ERROR: 需要传一个filename参数" 
    exit 1
fi
if [ ! -e $filename ];then
    echo "ERROR: $filename 文件不存在" 
    exit 1
fi

cp $filename $filename.bak
echo "源数据已备份"

while true
do
    end1=$(sed -n '$=' $filename)
    while true
    do
        head -n $batch_size $filename >> $filename.temp
        tail -n $batch_size $filename >> $filename.temp
        sort $filename.temp | uniq >> $filename.res
        rm -f $filename.temp
        sed -i "1,$batch_size d" $filename
        end=$(sed -n '$=' $filename)
        if [ ! $end ];then
            echo "待处理：0"
            break
        fi
        echo "待处理：$end"
        start=$(($end-$batch_size_flag))
        if [ $start -le 0 ];then
            start=1
        fi
        sed -i "$start,$end d" $filename
    done
    mv $filename.res $filename
    end2=$(sed -n '$=' $filename)
    if [ $end1 == $end2 ];then
        break
    fi
done
echo "处理完成！！！"